import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import altair as alt
from modules import spec_tools
from pybaselines import Baseline, utilsSpec_Tools Tutorial
This jupyter script is used as a showcase for the spec_tools toolbox, which can be used for the analysis of IR Spectra using Python.
The Spectrum Class
The Spectrum Class is the main object of the module. An object of the spectrum class can be initialized by using the corresponding method from the class
spec_tools.Spctrum.from_csv("filepath",sep=";", comma=",")
Spectrum = spec_tools.Spectrum.from_csv("shee2.csv",sep=";",comma=",")A simple plot of the whole spectrum can be done with the plot_spectrum function. A additional header may be given
Spectrum.plot_spectrum(title="Hello")/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
By using a loop one can extract different spectral windows out of the spectrum
spectral_windows = [[2200,2215],[2175,2190]]
spectrums = []
for window in spectral_windows:
spectrum_window = Spectrum.split_spectral_window(window)
spectrums.append(spectrum_window)
spectrum1 = spectrums[0]
spectrum2 = spectrums[1]
spectrum1.plot_spectrum(title="Spectrum 1")/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
The Baseline Class
Using an Instance of the Spectrum Class one can initialize the Baseline Class and fit/obtain different baselines.
For this them package pybaselines is used
Using spectrum1 we now try out different fitting procedures
Baseline1 = spec_tools.Baseline(spectrum1)
fit, params = Baseline1.fit_baseline_asls(lam=10000,tol=1e-5,max_iter=1000)
# This Baseline now be plotted under the spectrum
Baseline1.plot_baseline_and_spectrum("Hello")
/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
The baseline can now be substracted yielding a new object of the spectrum class
Spectrum1_corr = Baseline1.substract_baseline()
Spectrum1_corr.interactive_integration()Integration using different deconvolution methods
chart, peaks = Spectrum1_corr.plot_spectrum_peak_picking(threshold = 0.001)
chart/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
# now given the chart we can initialize the Deconvole Object
Deconvolution = spec_tools.Deconvolution(Spectrum1_corr,peaks)
print(Deconvolution.peaks)
fit = Deconvolution.fit_gaussian(Deconvolution.peaks[2])
Deconvolution.plot_gaussian_fit(Deconvolution.peaks[2])[57 81 96]

Deconvolution.fit_multiple_gaussians_and_plot(broadness=3)/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
/opt/anaconda3/lib/python3.11/site-packages/altair/utils/core.py:395: FutureWarning: the convert_dtype parameter is deprecated and will be removed in a future version. Do ``ser.astype(object).apply()`` instead if you want ``convert_dtype=False``.
col = df[col_name].apply(to_list_if_array, convert_dtype=False)
# Calculate Integrals of the Fitted Functions
integrals = Deconvolution.calculate_integral_of_fits()
# Now we can plot the integrals
ratios = spec_tools.ratios_of_integrals(integrals,2)
print(ratios)
plt.bar(range(len(integrals)),integrals)
plt.show()[0.08226189768121268, 0.4693617068585285, 1.0]

Integration using the Derivative Spectrum:
- Goal ist to find the integration boundaries for the different peaks
- For this we first generate the derivative spectrum as a object
Spectrum1_corr_derivative = Spectrum1_corr.derivative_spectrum()
Spectrum1_corr_derivative.plot_spectrum(title="Derivative")
Spectrum1_corr_derivative_2 = Spectrum1_corr_derivative.derivative_spectrum()/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
Spectrum1_corr_derivative.plot_derivative(title="Derivative")
# Obtain Peaks from second derivative
peaks = Spectrum1_corr_derivative_2.find_maxima(threshold=0.05)
print(peaks)
Spectrum1_corr_derivative_2.plot_spectrum_with_peaks(peaks)[ 91 100 81 96]
# We can now visualize this peaks in the original spectrum
Spectrum1_corr_derivative.plot_spectrum_with_peaks(peaks)/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
Spectrum Plotting
For this the
Spectrum = spec_tools.Spectrum.from_csv("shee2.csv",sep=";",comma=",")
Spectrum.plot_spectrum(title="Hello")
/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
Plot Spectral Window
By using the plot_spectral_window method, one can plot the spectral window of the spectrum
spectrum.plot_spectral_window()Spectrum.plot_spectral_window(title="Hello",x_min=2150, x_max=2250)/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
# Perform peackpicking in spectral window
Spectrum.plot_spectral_window_peak_picking(title="Hello,",x_min=1000, x_max=2250, threshold=0.1)
/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
(alt.LayerChart(...), alt.Chart(...))
chart, integral = Spectrum.calculate_integral(x_min=1000, x_max=2250)
chart/opt/anaconda3/lib/python3.11/site-packages/altair/utils/core.py:395: FutureWarning: the convert_dtype parameter is deprecated and will be removed in a future version. Do ``ser.astype(object).apply()`` instead if you want ``convert_dtype=False``.
col = df[col_name].apply(to_list_if_array, convert_dtype=False)
Plot Multiple Spectral Windows
ls_windows = [[2200,2215],[2175,2190]]
subtitles = ["Window 1", "Window 2"]
Spectrum.plot_multiple_spectral_windows(ls_of_windows=ls_windows, title="Nice Shit",subtitles=subtitles)/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
# Calculate Integral of Multiple peaks
chart, areas = Spectrum.integrate_multiple_spectral_windows(ls_of_windows=ls_windows)
chart/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
/opt/anaconda3/lib/python3.11/site-packages/altair/utils/core.py:395: FutureWarning: the convert_dtype parameter is deprecated and will be removed in a future version. Do ``ser.astype(object).apply()`` instead if you want ``convert_dtype=False``.
col = df[col_name].apply(to_list_if_array, convert_dtype=False)
/opt/anaconda3/lib/python3.11/site-packages/altair/utils/core.py:395: FutureWarning: the convert_dtype parameter is deprecated and will be removed in a future version. Do ``ser.astype(object).apply()`` instead if you want ``convert_dtype=False``.
col = df[col_name].apply(to_list_if_array, convert_dtype=False)
Spectrum.plot_derivative(title="Hello")/opt/anaconda3/lib/python3.11/site-packages/altair/utils/deprecation.py:65: AltairDeprecationWarning: 'add_selection' is deprecated. Use 'add_params' instead.
warnings.warn(message, AltairDeprecationWarning, stacklevel=1)
Baseline Correction
Baseline = spec_tools.Baseline(Spectrum)
Baseline.fit_baseline_asls(lam=5e6, tol=1e-3, max_iter=20)
(array([ 0.01110359, 0.01110664, 0.0111097 , ..., -0.0405984 ,
-0.04062492, -0.04065144]),
{'weights': array([0.01, 0.01, 0.01, ..., 0.01, 0.01, 0.01]),
'tol_history': array([0.68991331, 0.72181646, 0.71097111, 0.61806805, 0.48002348,
0.34684309, 0.25761441, 0.26805353, 0.17193777, 0.11523654,
0.06436087, 0.01562341, 0. ])})